/*
 * $Id: LocationRepository.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.data.repository;

import com.sci.logisticsmap.data.model.LocationModel;
import com.sci.logisticsmap.data.support.sorting.LocationModelSorting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.QueryUtils;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * The Repository for accessing Location models.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
@Repository
public class LocationRepository extends BaseRepository<LocationModel, String> {
//public interface LocationRepository extends BaseRepository<LocationModel, String> {
    /**
     * Initializes a Repository.
     *
     * @param factory The MongoDB Repository factory.
     * @param mongoOperations The MongoDB operations.
     */
    @Autowired
    public LocationRepository(MongoRepositoryFactory factory, MongoOperations mongoOperations) {
        super(factory.<LocationModel, String>getEntityInformation(LocationModel.class), mongoOperations);
    }

    /**
     * Gets the Location models by the Geodata id.
     *
     * @param geodataId The Geodata id.
     *
     * @return The Location models.
     */
    public List<? extends LocationModel> findByGeodataId(String geodataId) {
        return findByGeodataId(geodataId, LocationModelSorting.BY_POSITION);
    }

    /**
     * Gets the Location models by the Geodata id and sorted by the specified option.
     *
     * @param geodataId The Geodata id.
     * @param sort The sorting option.
     *
     * @return The Location models.
     */
    public List<? extends LocationModel> findByGeodataId(String geodataId, Sort sort) {
        Query query = Query.query(Criteria.where("geodataId").is(geodataId));
        if (sort != null) {
            QueryUtils.applySorting(query, sort);
        }
        return getMongoOperations().find(query, LocationModel.class);
    }
}
